package _base0to1;

/**
 * 859. 亲密字符串
 */
public class No859 {
    public boolean buddyStrings(String s, String goal) {
        int n = s.length();
        int m = goal.length();
        if (n != m) return false;
        int num = 0;
        int[] sChar = new int[26];
        int[] goalChar = new int[26];
        for (int i = 0; i < n; i++) {
            char c1 = s.charAt(i);
            char c2 = goal.charAt(i);
            sChar[c1 - 'a']++;
            goalChar[c2 - 'a']++;
            if (c1 != c2) num++;
        }
        if (num == 0) for (int i : sChar) if (i >= 2) return true;
        if (num == 2) for (int j = 0; j < 26; j++) if (sChar[j] != goalChar[j]) return false;
        return num == 2;
    }
}
